<template>
  <div>
    <el-card>
      <!--      三级列表选择器-->
      <CategorySelect @getAttrList="getSpuList" :show="scene==0"></CategorySelect>
    </el-card>
    <el-card shadow="hover" style="margin: 20px 0px;">
      <!--      //表格元素-->
      <div v-show="scene==0">
        <el-button type="primary" icon="el-icon-plus" style="margin:0px 0px 20px 0px" @click="addSpu">添加Spu</el-button>
        <el-table border style="width: 100%" :data="records">
          <el-table-column label="序号" type='index' width="150px" align="center"></el-table-column>
          <el-table-column label="Spu名称" prop="spuName" width="width"></el-table-column>
          <el-table-column label="Spu描述" prop="description" width="width"></el-table-column>
          <el-table-column label="操作" prop="" width="width">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-plus" size="mini" title='添加sku' @click="addSku(scope.row)"></el-button>
              <el-button type="warning" icon="el-icon-edit" size="mini" title="修改spu" @click="update(scope.row)"></el-button>
              <el-button type="info" icon="el-icon-info" size="mini" title="查看spu信息" @click="viewSkuList(scope.row)"></el-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(scope.row)" style="margin:10px">
                <el-button type="danger" icon="el-icon-delete" size="mini" title="删除spu" slot="reference"></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          align="center"
          :current-page="currentPage"
          :page-sizes="[3, 5, 10]"
          :page-size="size"
          layout=" prev, pager, next, jumper, ->,sizes, total, slot"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
<!--      sku表单和spu表单的切换-->
      <SkuForm v-show="scene==1" ref="sku" @cancelSkuForm="changeScene"></SkuForm>
      <SpuFrom v-show="scene==2" ref="spu" @cancelSpuForm="changeScene"></SpuFrom>
    </el-card>

    <el-dialog :title="`${dialogTitle}的sku列表`" :visible.sync="dialogTableVisible" :before-close="beforeClose">
      <el-table :data="skuList" v-loading="loading">
        <el-table-column property="skuName" label="名称" width="width" ></el-table-column>
        <el-table-column property="price" label="价格" width="width" align="center"></el-table-column>
        <el-table-column property="weight" label="重量" width="width" align="center"></el-table-column>
        <el-table-column  label="默认图片" width="width" align="center">
          <template slot-scope="{row,$index}" >
            <img :src="row.skuDefaultImg" style="width:120px;height:120px;">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SkuForm from './SkuForm/index.vue'
import SpuFrom from './SpuForm/index.vue'
import {reqDeleteSpuInfo} from "@/api/Product/spu";
export default {
  name: 'Spu',
  components:{
    SpuFrom,
    SkuForm
  },
  data() {
    return {
      //分区器参数
      pages: 1,//总页数
      size: 3,//页面数据条数
      records: [],//trademarkList
      currentPage: 1,//当前页
      total: 10,//数据总条数

      //三级分类列表属性
      IdForm:{
        Category1Id:"",
        Category2Id:"",
        Category3Id:"",
      },

      //属性列表table是否展示
      isShowTable: true,

      //表单元素切换的控制参数
      scene:0,//0 表单展示页；1：spu添加页；2：sku添加页

      //控制sku列表展示dialog的参数
      dialogTableVisible:false,

      //sku列表信息
      skuList:[],
      dialogTitle:'',

      //加载显示动效loading
      loading:true,
    }
  },
  methods: {
    async getSpuList(IdForm) {
      this.IdForm=IdForm;
      // console.log('发请求！');
      //解构IdForm
      const result = await this.$API.default.spu.reqSpuList(this.currentPage, this.size,this.IdForm.Category3Id);
      // console.log(result)
      if (result.code == 200) {
        this.pages = result.data.pages;
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    //查看其他页
    handleCurrentChange(page) {
      this.currentPage = page;
      this.getSpuList(this.IdForm);
    },
    //修改每页展示数据size
    handleSizeChange(size) {
      this.size = size;
      this.getSpuList(this.IdForm);
    },
    //切换到spuForm表单的回调
    update(row){
      this.scene=2;
      // console.log('row：',row);
      // console.log(this.$refs.spu)
      this.$refs.spu.initSpuFormData(row.id);
    },
    //添加spu的回调
    addSpu(){
      this.scene=2;
      //点击添加spu按钮，控制子组件开始初始化数据
      this.$refs.spu.addInitSpuFormData(this.IdForm.Category3Id);
    },
    //子组件改变父组件scene的数据，切换table/form
    changeScene(data){
      this.scene=data.scene;
      if(data.page){
        this.currentPage=data.page;
      }
      this.getSpuList(this.IdForm);
    },
    //删除属性列表按钮的回调
    async deleteSpu(row){
      const result=await this.$API.default.spu.reqDeleteSpuInfo(row.id);
      // console.log(result)
      if(result.code==200){
        //重新刷新页面
        this.getSpuList(this.IdForm)

        this.$message({
          type:'success',
          message:'删除成功'
        })
      }
    },
    //添加sku按钮的回调
    addSku(row){
      this.scene=1;
      // console.log(row);
      const {Category1Id,Category2Id}=this.IdForm;
      this.$refs.sku.initSkuData(Category1Id,Category2Id,row);
    },

    //查看sku列表按钮的回调
    async viewSkuList(row){
      this.dialogTableVisible=true;
      this.dialogTitle=row.spuName;
      //发起请求获取数据
      const result=await this.$API.default.spu.reqSkuList(row.id);
      // console.log(result);
      if(result.code==200){
        this.skuList=result.data;
        this.loading=false;
      }
    },
    //关闭dialog前的回调，清除skuList数据，重置loading
    beforeClose(done){
      this.loading=true;
      this.skuList=[];
      done();
    },
  }
}
</script>

<style scoped>

</style>
